package controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import pojo.AdminMessage;
import pojo.UserMessage;
import service.AdminBookService;
import service.UserBookService;

import javax.servlet.http.HttpServletRequest;

/**
 * @author chen
 * @author XingTian
 * ********************
 */
@Controller
public class login {
    @Autowired
    AdminBookService adminBookService;
    @Autowired
    UserBookService userBookService;

    /**
     * 判断用户登录身份，根据身份的不同，跳转到不同的功能页面
     */
    @RequestMapping("/choose")
    public String choose(@RequestParam String identity) {
        String str;
        if (identity.compareTo("管理员登录") == 0) {
            str = "redirect:/toadmin";
        } else if (identity.compareTo("用户注册") == 0) {
            str = "/register";
        } else {
            str = "redirect:/touser";
        }
        return str;
    }

    @RequestMapping("/toadmin")
    public String adminlogin() {
        return "admin";
    }

    /**
     * 管理员登录
     */
    @RequestMapping("/adminlog")
    @ResponseBody
    public String adminlog(String controllerName, String controllerPassword, String code, HttpServletRequest req) {
        req.getSession().setAttribute("adminName", controllerName);
        String Code = (String) req.getSession().getAttribute("code");
        AdminMessage admin = adminBookService.getAdminMessage(controllerName, controllerPassword);
        String str = "";
        if (Code.compareTo(code) != 0) {
            str += "验证码错误";
        } else if (admin == null) {
            str += "账号或密码错误";
        } else {
            str += "1";
        }
        return str;
    }

    /**
     * 得到用户名
     * 更新欢迎信息
     */
    @RequestMapping("/getName")
    @ResponseBody
    public String getName(HttpServletRequest req) {
        int action = Integer.parseInt(req.getParameter("action"));
        String userName = null;
        if (action == 1) {
            userName = (String) req.getSession().getAttribute("userName");
        }
        if (action == 2) {
            userName = (String) req.getSession().getAttribute("adminName");
        }
        return userName;
    }

    /**
     * 去到用户注册页面
     */
    @RequestMapping("toregister")
    public String toregister() {
        return "register";
    }

    /**
     * 用户注册
     */
    @RequestMapping("/register")
    @ResponseBody
    public String register(String code, UserMessage user, HttpServletRequest req) {
        String Code = (String) req.getSession().getAttribute("code");
        String str = "";
        if (code.compareTo(Code) == 0) {
            int d = userBookService.AddUser(user);
            if (d == 0) {
                str += "用户名已存在";
            } else {
                str += "1";
            }
        } else {
            str += "验证码错误";
        }
        return str;
    }

    /**
     * 去到用户登录页面
     */
    @RequestMapping("/touser")
    public String userlogin() {
        return "user";
    }

    /**
     * 用户登录
     */
    @RequestMapping("/userlogin")
    @ResponseBody
    public String userlogin(String userName, String userPassword, String code, HttpServletRequest req) {
        String Code = (String) req.getSession().getAttribute("code");
        String str = "";
        req.getSession().setAttribute("userName", userName);
        UserMessage user = userBookService.login(userName, userPassword);
        if (Code.compareTo(code) != 0) {
            str += "验证码错误";
        } else if (user == null) {
            str += "账号或密码错误";
        } else {
            str += "1";
        }
        return str;
    }
}
